  package com.e2e.haiti.infrastructure.surveyor;

import com.actionbarsherlock.app.SherlockFragment;
import com.e2e.haiti.infrastructure.surveyor.R;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class FragmentLayoutQuestionnaire extends SherlockFragment  {
	
    private Utils utilsHelper = null;
    private Cursor structureCursor = null;
    private int structureID;
	
	private EditText EditText_NumberOfRooms  = null;    
    private EditText EditText_PlanDimensionsWidth= null;
    private EditText EditText_PlanDimensionsDepth= null;
    
    private Spinner Spinner_nLevels = null;
    
    private static final int NUMBER_OF_LEVELS_ADAPTOR_ID = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
                
        utilsHelper = new Utils(getActivity().getApplicationContext());      
                  
    } // end of the onCreate method
    
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
    	
    	// Sets the cursor
        setStructureCursor(); 
    	
    	View fragmentView = inflater.inflate(R.layout.layout_questionnaire_layout, container, false);
    	manageFormFields(fragmentView);
    	
    	return fragmentView;
    }
    
    private void setStructureCursor(){
        utilsHelper.openDatabase(); 
   	   // Get structure ID
          structureID = getActivity().getIntent().getIntExtra("structureID", -1); //Get structureID otherwise -1      
          structureCursor = utilsHelper.getStructureCursor(structureID); 
          utilsHelper.closeDatabase(); 
      }
    
    
    private void manageFormFields(View v){
    	
    	// Declares the text input fields
		EditText_NumberOfRooms  = (EditText) v.findViewById(R.id.Layout_editText_NumberOfRooms);  
	    EditText_PlanDimensionsWidth= (EditText) v.findViewById(R.id.Layout_editText_PlanDimensionsWidth);  
	    EditText_PlanDimensionsDepth= (EditText) v.findViewById(R.id.Layout_editText_PlanDimensionsDepth);  
         
        // Checks if data already exists
        String nRooms = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_ROOMS));
		String planDimensionsWidth = structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_PLAN_DIMENSIONS_WIDTH));
		String planDimensionsDepth= structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_PLAN_DIMENSIONS_DEPTH));

		// Sets the text of the input fields
		EditText_NumberOfRooms.setText(nRooms);
	    EditText_PlanDimensionsWidth.setText(planDimensionsWidth);
	    EditText_PlanDimensionsDepth.setText(planDimensionsDepth);
	    	    
        // Initializes view spinners
        initializeSpinners(v);
    }   
    
 private void initializeSpinners(View v){
    	
// Number of Levels Array Adapter ========================================================================================================================
       ArrayAdapter<CharSequence> adapter_nLevels= ArrayAdapter.createFromResource(
       		getActivity().getApplicationContext(), R.array.nLevels, android.R.layout.simple_spinner_item );
       		adapter_nLevels.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item);

       		Spinner_nLevels = (Spinner) v.findViewById(R.id.Layout_spinner_NumberOfLevels);
       		Spinner_nLevels.setAdapter( adapter_nLevels );
     			
     		//  Sets the location of spinner to saved value if there is one\
     			int nLevelsPosition = getArrayPosition(NUMBER_OF_LEVELS_ADAPTOR_ID);
     			Spinner_nLevels.setSelection(nLevelsPosition, true);  
    }
 
 	private int getArrayPosition(int spinnerType){
 		
 		int position=0;
 		
 		switch (spinnerType){
 		
 			case NUMBER_OF_LEVELS_ADAPTOR_ID:
 				
 				ArrayAdapter nLevels_Adapter = (ArrayAdapter) Spinner_nLevels.getAdapter();			
 	 			String nLevelsValue= structureCursor.getString(structureCursor.getColumnIndex(SqlHelper.COLUMN_STRUCTURE_NUMBER_OF_LEVELS));
 	 			position = nLevels_Adapter.getPosition(nLevelsValue);
 				
 				break;
 				
 			default:				
 				break;
 			 		 		 		
 		}							
 		
 		return position;
 	}
   
} 